package de.dhbw.horb.inf2013.vierFarbenProblem.viewModel.datatype;

import java.util.HashMap;
import java.util.Map;

import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Vertex;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.VertexNotSetException;

/**
 * The Class AdjacencyListGraph.
 */
public class AdjacencyListGraph implements Graph {

	/** The vertexes. */
	private Map<Integer, Vertex> vertexes = new HashMap<Integer, Vertex>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Graph#addVertex
	 * (de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.ColoredVertex)
	 */
	@Override
	public void addVertex(Vertex v) {
		this.vertexes.put(v.getId(), v);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Graph#addEdge(int,
	 * int)
	 */
	@Override
	public void addEdge(int from, int to) throws VertexNotSetException {
		if (!this.vertexes.containsKey(from) || !this.vertexes.containsKey(to)) {
			throw new VertexNotSetException();
		}

		this.vertexes.get(from).addNeighbor(to);
		this.vertexes.get(to).addNeighbor(from);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Graph#getVertex
	 * (int)
	 */
	@Override
	public Vertex getVertex(int id) throws VertexNotSetException {

		if (!vertexes.containsKey(id)) {
			throw new VertexNotSetException();
		}

		return vertexes.get(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Graph#containsVertex
	 * (int)
	 */
	@Override
	public boolean containsVertex(int id) {
		return vertexes.containsKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Graph#resetAll()
	 */
	@Override
	public void resetAll() {
		vertexes.clear();
	}

	public int countVertexes() {
		return vertexes.size();
	}
}
